package com.lx.demo.b.core.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.lx.demo.b.core.properties.DataSourceProperties;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.sql.SQLException;

/***
 * create by lanyxp on 2020/6/19 0019
 */
@Configuration
@Slf4j
public class DataSourceConfig {

    private final DataSourceProperties propertiess;

    public DataSourceConfig(DataSourceProperties propertiess) {
        this.propertiess = propertiess;
    }

    @Bean
    public DataSource dataSource() {
        DruidDataSource datasource = new DruidDataSource();
        datasource.setUrl(propertiess.getUrl());
        datasource.setUsername(propertiess.getUsername());
        datasource.setPassword(propertiess.getPassword());
        datasource.setDriverClassName(StringUtils.isBlank(propertiess.getDriverClassName()) ? "com.mysql.jdbc.Driver" : propertiess.getDriverClassName());
        datasource.setInitialSize(propertiess.getInitialSize());
        datasource.setMinIdle(propertiess.getMinIdle());
        datasource.setMaxActive(propertiess.getMaxActive());
        datasource.setMaxWait(propertiess.getMaxWait());
        datasource.setTimeBetweenEvictionRunsMillis(propertiess.getTimeBetweenEvictionRunsMillis());
        datasource.setMinEvictableIdleTimeMillis(propertiess.getMinEvictableIdleTimeMillis());
        datasource.setValidationQuery(propertiess.getValidationQuery());
        datasource.setTestWhileIdle(propertiess.isTestWhileIdle());
        datasource.setTestOnBorrow(propertiess.isTestOnBorrow());
        datasource.setTestOnReturn(propertiess.isTestOnReturn());
        datasource.setPoolPreparedStatements(propertiess.isPoolPreparedStatements());
        datasource.setMaxPoolPreparedStatementPerConnectionSize(propertiess.getMaxPoolPreparedStatementPerConnectionSize());
        try {
            datasource.setFilters(propertiess.getFilters());
        } catch (SQLException e) {
            log.error("druid configuration initialization filter", e);
        }
        datasource.setConnectionProperties(propertiess.getConnectionProperties());
        return datasource;
    }
    /*@Bean
    public ServletRegistrationBean druidServlet() {
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
        servletRegistrationBean.addInitParameter("loginUsername", "root");
        servletRegistrationBean.addInitParameter("loginPassword", "root");
        return servletRegistrationBean;
    }*/
    /*@Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new WebStatFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        return filterRegistrationBean;
    }*/
}
